﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PGI.EN.Reportes;
using PGI.LN.Reportes;

namespace PGI.WF
{
    public partial class FrmReporteVentas : Form
    {
        private bool siBusco = false;

        public FrmReporteVentas()
        {
            InitializeComponent();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            if (fechas_validas(dtpFechaInicial.Value, dtpFechaFinal.Value))
            {
                siBusco = true;
                List<RptVentasEn> lstReporte = new List<RptVentasEn>();
                lstReporte = ReportesLn.Reporte_Ventas(String.Format("{0:yyyyMMdd}", dtpFechaInicial.Value), String.Format("{0:yyyyMMdd}", dtpFechaFinal.Value));

                dgvRptVentas.DataSource = lstReporte;
            }
        }

        private bool fechas_validas(DateTime fechaInicio, DateTime fechaFin)
        {
            if (dtpFechaInicial.Value.Date > dtpFechaFinal.Value.Date)
            {
                MessageBox.Show(this, "La fecha de fin debe ser mayor o igual a la fecha de inicio", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }

        private void btnExportarExcel_Click(object sender, EventArgs e)
        {
            if (siBusco)
            {
                Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

                excel.Application.Workbooks.Add(true);
                int ColumnIndex = 0;

                foreach (DataGridViewColumn col in dgvRptVentas.Columns)
                {
                    ColumnIndex++;
                    excel.Cells[1, ColumnIndex] = col.Name;
                }
                int rowIndex = 0;

                foreach (DataGridViewRow row in dgvRptVentas.Rows)
                {
                    rowIndex++;
                    ColumnIndex = 0;

                    foreach (DataGridViewColumn col in dgvRptVentas.Columns)
                    {
                        ColumnIndex++;
                        excel.Cells[rowIndex + 1, ColumnIndex] = row.Cells[col.Name].Value;
                    }
                }
                excel.Visible = true;
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.ActiveSheet;
                worksheet.Activate();
            }
        }


    }
}
